﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using System.Security.Cryptography;

namespace PRE.Catalog
{
    public partial class FrmGiuachucvu : DevExpress.XtraEditors.XtraForm
    {
        #region Variable
        DAL.EntitiesDAL.GiuchucvuDAL _dal = new DAL.EntitiesDAL.GiuchucvuDAL();
        BLL.EntitiesBLL.GiuchucvuBLL _bll = new BLL.EntitiesBLL.GiuchucvuBLL();

        DataTable _dtb = new DataTable();
        DataTable _dtb1 = new DataTable();
        DataTable _dtb2 = new DataTable();
        #endregion

        #region Inist
        public FrmGiuachucvu()
        {
            InitializeComponent();
        }
        #endregion

        #region ReadOnlyControl
        protected void ReadOnlyControl(bool isReadOnly = true)
        {
            cbo_Machucvu.Properties.ReadOnly = isReadOnly;
            cbo_Manhanvien.Properties.ReadOnly = isReadOnly;
            dte_Ngaybatdau.Properties.ReadOnly = isReadOnly;
        }
        #endregion

        #region ClearDataBindings
        protected void ClearDataBindings()
        {
            cbo_Machucvu.DataBindings.Clear();
            cbo_Manhanvien.DataBindings.Clear();
            dte_Ngaybatdau.DataBindings.Clear();
        }
        #endregion

        #region DataBindingControl
        protected void DataBindingControl()
        {
            cbo_Machucvu.DataBindings.Add("EditValue", _dtb, ".MACHUCVU");
            cbo_Manhanvien.DataBindings.Add("EditValue", _dtb, ".MANHANVIEN");
            dte_Ngaybatdau.DataBindings.Add("EditValue", _dtb, ".NGAYBATDAU");
        }
        #endregion

        #region PerformRefresh
        protected void PerformRefresh()
        {
            ReadOnlyControl();
            cbo_Machucvu.ResetText();
            cbo_Manhanvien.ResetText();
            dte_Ngaybatdau.ResetText();
            LoadData();

            if (_dtb != null)
            {
                ClearDataBindings();
                if (_dtb.Rows.Count > 0) DataBindingControl();
            }

        }
        #endregion

        #region LoadData
        protected void LoadData()
        {
            bto_Luu.Visible = false;
            bto_Huy.Visible = false;
            _dtb1 = _dal.Select_Chucvu();
            _dtb2 = _dal.Select_Nhanvien();
            cbo_Machucvu.Properties.DataSource = _dtb1;
            cbo_Manhanvien.Properties.DataSource = _dtb2;
            try
            {
                _dtb = _dal.Select();

                if (_dtb != null)
                {
                    grcGiuchucvu.DataSource = _dtb;

                    AppGridView.SummaryField(grvGiuchucvu, "MANHANVIEN", "Tổng = {0:#,0} Mẫu tin", DevExpress.Data.SummaryItemType.Count);

                    grvGiuchucvu.OptionsView.ColumnAutoWidth = true;
                }
            }
            catch (Exception ex)
            {
                XtraMessageBox.Show("Lỗi : " + ex.Message, "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }
        #endregion

        #region Form Load
        private void grcGiuchucvu_Load(object sender, EventArgs e)
        {              
            PerformRefresh();
        }
        #endregion

        #region Thoát
        private void bto_Thoat_Click(object sender, EventArgs e)
        {
            this.Close();
        }
        #endregion

        #region Thêm
        private void bto_Them_Click(object sender, EventArgs e)
        {
            _dtb1 = _dal.Select_Chucvu();
            _dtb2 = _dal.Select_Nhanvien();
            cbo_Machucvu.Properties.DataSource = _dtb1;
            cbo_Manhanvien.Properties.DataSource = _dtb2;
            dte_Ngaybatdau.EditValue = DateTime.Now;
            cbo_Machucvu.Properties.ReadOnly = false;
            cbo_Manhanvien.Properties.ReadOnly = false;
            dte_Ngaybatdau.Properties.ReadOnly = false;

            bto_Them.Visible = false;
            bto_Xoa.Visible = false;
            bto_Luu.Visible = true;
            bto_Huy.Visible = true;
        }
        #endregion

        #region Xóa
        private void bto_Xoa_Click(object sender, EventArgs e)
        {
            var id1 = grvGiuchucvu.GetFocusedRowCellValue("MACHUCVU") + "";
            var id2 = grvGiuchucvu.GetFocusedRowCellValue("MANHANVIEN") + "";
            DialogResult result;
            var ok = _dal.Delete_Giuchucvu(id1, id2);
            result = XtraMessageBox.Show("Bạn có chắc xóa không ? ", "Thông Báo", MessageBoxButtons.OKCancel, MessageBoxIcon.Warning);
            if (result == DialogResult.OK)
            {
                if (ok)
                {
                    PerformRefresh();
                }
                else
                    XtraMessageBox.Show("Lỗi! Không xóa được", "Thông Báo", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }
        #endregion

        #region Lưu
        private void bto_Luu_Click(object sender, EventArgs e)
        {
            var o = new DAL.Entities.Giuchucvu()
            {
                MACHUCVU = Convert.ToString(cbo_Machucvu.EditValue),
                MANHANVIEN = Convert.ToString(cbo_Manhanvien.EditValue),
                NGAYBATDAU = Convert.ToDateTime(dte_Ngaybatdau.EditValue)
            };

            var oki = _dal.Insert(o);
            if (oki)
            {
                XtraMessageBox.Show("Đã lưu thành công", "Thông Báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
                PerformRefresh();
                bto_Them.Visible = true;
                bto_Xoa.Visible = true;
            }
            else
            {
                XtraMessageBox.Show("Lỗi! Lưu thất bại", "Thông Báo", MessageBoxButtons.OK, MessageBoxIcon.Error);

            }
        }
        #endregion

        #region Hủy
        private void bto_Huy_Click(object sender, EventArgs e)
        {
            PerformRefresh();
            bto_Huy.Visible = false;
            bto_Luu.Visible = false;
            bto_Them.Visible = true;
            bto_Xoa.Visible = true;
            cbo_Machucvu.Properties.ReadOnly = true;
            cbo_Manhanvien.Properties.ReadOnly = true;
            dte_Ngaybatdau.Properties.ReadOnly = true;
        }
        #endregion
    }
}
